﻿using SignalBox.Core.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing;

namespace SignalBoxSimulator
{
    public class TrainDetails : DockContent
    {

        public TrainDetails()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.trainDetailBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.locationDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.arrivalDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.departureDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.commentDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            ((System.ComponentModel.ISupportInitialize)(this.trainDetailBindingSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // trainDetailBindingSource
            // 
            this.trainDetailBindingSource.DataSource = typeof(SignalBox.Core.Model.TrainDetail);
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.locationDataGridViewTextBoxColumn,
            this.arrivalDataGridViewTextBoxColumn,
            this.departureDataGridViewTextBoxColumn,
            this.commentDataGridViewTextBoxColumn});
            this.dataGridView1.DataSource = this.trainDetailBindingSource;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.Size = new System.Drawing.Size(454, 262);
            this.dataGridView1.TabIndex = 0;
            // 
            // locationDataGridViewTextBoxColumn
            // 
            this.locationDataGridViewTextBoxColumn.DataPropertyName = "Location";
            this.locationDataGridViewTextBoxColumn.HeaderText = "Location";
            this.locationDataGridViewTextBoxColumn.Name = "locationDataGridViewTextBoxColumn";
            this.locationDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // arrivalDataGridViewTextBoxColumn
            // 
            this.arrivalDataGridViewTextBoxColumn.DataPropertyName = "Arrival";
            this.arrivalDataGridViewTextBoxColumn.HeaderText = "Arrival";
            this.arrivalDataGridViewTextBoxColumn.Name = "arrivalDataGridViewTextBoxColumn";
            this.arrivalDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // departureDataGridViewTextBoxColumn
            // 
            this.departureDataGridViewTextBoxColumn.DataPropertyName = "Departure";
            this.departureDataGridViewTextBoxColumn.HeaderText = "Departure";
            this.departureDataGridViewTextBoxColumn.Name = "departureDataGridViewTextBoxColumn";
            this.departureDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // commentDataGridViewTextBoxColumn
            // 
            this.commentDataGridViewTextBoxColumn.DataPropertyName = "Comment";
            this.commentDataGridViewTextBoxColumn.HeaderText = "Comment";
            this.commentDataGridViewTextBoxColumn.Name = "commentDataGridViewTextBoxColumn";
            this.commentDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // TrainDetails
            // 
            this.ClientSize = new System.Drawing.Size(454, 262);
            this.Controls.Add(this.dataGridView1);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "TrainDetails";
            this.TabText = "Train details";
            ((System.ComponentModel.ISupportInitialize)(this.trainDetailBindingSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.BindingSource trainDetailBindingSource;
        private System.ComponentModel.IContainer components;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.DataGridViewTextBoxColumn locationDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn arrivalDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn departureDataGridViewTextBoxColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn commentDataGridViewTextBoxColumn;

        private TrainInfo _trainInfo;

        public TrainInfo TrainInfo
        {
            get { return _trainInfo; }
            set { _trainInfo = value; FillData(); }
        }

        private void FillData()
        {
            if (_trainInfo == null)
            {
                trainDetailBindingSource.DataSource = null;
                return;
            }

            List<TrainDetail> details = new List<TrainDetail>();

            foreach (TrainStop ts in TrainInfo.Train.Stops)
            {

                if (ts.Action == TrainStop.TrainAction.ENTER)
                {
                    TrainDetail td = new TrainDetail();
                    td.Location = ts.Location;
                    td.Arrival = ts.Arrive.ToString("HH:mm");
                    td.Departure = "-";
                    td.Comment = "";
                    details.Add(td);
                }
                else if (ts.Action == TrainStop.TrainAction.EXIT)
                {
                    TrainDetail td = new TrainDetail();
                    td.Location = ts.Location;
                    td.Arrival = ts.Arrive.ToString("HH:mm");
                    td.Departure = "-";
                    td.Comment = "";
                    details.Add(td);
                }
                else if (ts.Action == TrainStop.TrainAction.STOP)
                {
                    TrainDetail td = new TrainDetail();
                    td.Location = ts.Location;
                    td.Arrival = ts.Arrive.ToString("HH:mm");
                    td.Departure = ts.Depart.ToString("HH:mm");
                    td.Comment = "";
                    details.Add(td);
                }
            }
            trainDetailBindingSource.DataSource = details;
        }
    }
}
